﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LacViet.ERP.DB;
using System.Configuration;

namespace tracker.Models
{
    public class DataClass : IDisposable
    {

        private AbstractDbFactory dbConn;
        private IDbTransaction _dbTrans;
        private string dbType;
        private string connectionString;

        public void Dispose()
        {
            Close();
            dbConn = null;
            _dbTrans = null;
        }
        public static AbstractDbFactory CreateConnection()
        {
            //TODO: Mã hóa ConnectionString
            string dbType;
            string connectionString;
            dbType = "mssql";
            connectionString = @"Data Source=.\SQLEXPRESS05;" +
                                       "database= BSOS;" +
                                       "User ID =sa;" +
                                       "Password =;" +
            "Integrated Security=SSPI;" +
           "Persist Security info =false";
            //connectionString = "workstation id=BSOSData.mssql.somee.com;packet size=4096;user id=NDHANH;pwd=123456789;data source=BSOSData.mssql.somee.com;persist security info=False;initial catalog=BSOSData";
            connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=BSOS;Integrated Security=True";
            AbstractDbFactory dbConn = AbstractDbFactory.getFactory(dbType);
            dbConn.open(connectionString);
            return dbConn;
        }

        public DataClass()
        {
            //TODO: Mã hóa ConnectionString
            dbType = "mssql";
            connectionString = @"Data Source=.\SQLEXPRESS05;" +
                                       "database= BSOS;" +
                                       "User ID =sa;" +
                                       "Password =;" +
            "Integrated Security=SSPI;" +
           "Persist Security info =false";
            //connectionString = "workstation id=BSOSData.mssql.somee.com;packet size=4096;user id=NDHANH;pwd=123456789;data source=BSOSData.mssql.somee.com;persist security info=False;initial catalog=BSOSData";
            //connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=BSOS;Integrated Security=True";
            connectionString = ConfigurationManager.ConnectionStrings["trackervn_connection"].ToString();
            //connectionString = @"server=PHU-PC\SQLEXPRESS; database=trackervn; uid=sa;pwd=1;Max Pool Size=1000;Min Pool Size=5";
            dbConn = AbstractDbFactory.getFactory(dbType);
        }
        public DataClass(AbstractDbFactory dbConn)
        {
            //TODO: Mã hóa ConnectionString
            dbType = "mssql";
            connectionString = @"Data Source=.\SQLEXPRESS05;" +
                                       "database=BSOS;" +
                                       "User ID =sa;" +
                                       "Password =;" +
            "Integrated Security=SSPI;" +
           "Persist Security info =false";
            //connectionString = "workstation id=BSOSData.mssql.somee.com;packet size=4096;user id=NDHANH;pwd=123456789;data source=BSOSData.mssql.somee.com;persist security info=False;initial catalog=BSOSData";
            connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=BSOS;Integrated Security=True";
            this.dbConn = dbConn;
            this._dbTrans = null;
        }
        public AbstractDbFactory Connection
        {
            get
            {
                return dbConn;
            }
        }
        public IDbTransaction Transaction
        {
            get
            {
                return _dbTrans;
            }
        }
        public bool Open()
        {
            return dbConn.open(connectionString);
        }

        public void Close()
        {
            try
            {
                dbConn.close();
            }
            catch
            {
            }
        }

        public IDbTransaction BeginTransaction()
        {
            _dbTrans = dbConn.BeginTransaction();
            return _dbTrans;
        }
    }
}
